package com.fulei.logic;

import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

public class EleLock
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  
  public static boolean OpenPort()
  {
    try
    {
      serialPort = new SerailPortOpt();
      
      serialPort.mDevNum = Integer.parseInt(comNo);
      serialPort.mSpeed = Integer.parseInt(baudRate);
      serialPort.mDataBits = 8;
      serialPort.mStopBits = 1;
      serialPort.mParity = 110;
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    try
    {
      if (serialPort.mFd != null) {
        serialPort.closeDev(serialPort.mFd);
      }
      serialPort.mFd = null;
      serialPort = null;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
  }
  
  public static int EleLockReset()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 126, 0, 5, 1, 18, 0, 0, 1, 20, -19 };
      
      LogUtil.writeEleLockLog("EleLockResetSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 10);
      if (ReadDataLength < 9) {
        return -1;
      }
      LogUtil.writeEleLockLog("EleLockResetRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] == 126) && (ReadData[9] == -19))
      {
        if ((ReadData[4] == -110) && (ReadData[7] == 1)) {
          return_data = 1;
        }
      }
      else {
        return -1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("电磁锁控制板复位异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int CheckEleLockState()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 126;
      SendData[1] = 0;
      SendData[2] = 5;
      SendData[3] = 1;
      SendData[4] = 5;
      SendData[5] = 0;
      SendData[6] = 0;
      SendData[7] = 0;
      SendData[8] = SumByte(SendData, 3, 5);
      SendData[9] = -19;
      
      LogUtil.writeEleLockLog("CheckEleLockStateSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 150, 11);
      if (ReadDataLength < 10) {
        return -1;
      }
      LogUtil.writeEleLockLog("CheckEleLockStateRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] == 126) && (ReadData[10] == -19)) {
        if (ReadData[4] == -123) {
          if (ReadData[7] == 0) {
            return_data = 1;
          } else if (ReadData[7] == 1) {
            return_data = 2;
          } else if (ReadData[7] == 2) {
            return_data = 3;
          } else if (ReadData[7] == 3) {
            return_data = 4;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("电磁锁查询状态接口异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int OpenOrCloseRightLock(int lockFlag)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 126;
      SendData[1] = 0;
      SendData[2] = 5;
      SendData[3] = 1;
      SendData[4] = 7;
      SendData[5] = 0;
      SendData[6] = ((byte)lockFlag);
      SendData[7] = 0;
      SendData[8] = SumByte(SendData, 3, 5);
      SendData[9] = -19;
//      byte[] SendData = new byte[10];
//      SendData[0] = 126;
//      SendData[1] = 0;
//      SendData[2] = 5;
//      SendData[3] = 1;
//      SendData[4] = 6;
//      SendData[5] = 0;
//      SendData[6] = ((byte)lockFlag);
//      SendData[7] = 0;
//      SendData[8] = SumByte(SendData, 3, 5);
//      SendData[9] = -19;
      LogUtil.writeEleLockLog("CheckEleLockStateSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 10);
      if (ReadDataLength < 9) {
        return -1;
      }
      LogUtil.writeEleLockLog("CheckEleLockStateRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] == 126) && (ReadData[9] == -19)) {
        if (ReadData[4] == -121) {
          if (ReadData[7] == 1) {
            return_data = 1;
          } else {
            return_data = -2;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("打开或关闭右侧电磁锁的指令接口异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int OpenOrCloseLeftLock(int lockFlag)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 126;
      SendData[1] = 0;
      SendData[2] = 5;
      SendData[3] = 1;
      SendData[4] = 6;
      SendData[5] = 0;
      SendData[6] = ((byte)lockFlag);
      SendData[7] = 0;
      SendData[8] = SumByte(SendData, 3, 5);
      SendData[9] = -19;
      
      LogUtil.writeEleLockLog("CheckEleLockStateSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 10);
      if (ReadDataLength < 9) {
        return -1;
      }
      LogUtil.writeEleLockLog("CheckEleLockStateRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] == 126) && (ReadData[9] == -19)) {
        if (ReadData[4] == -122) {
          if (ReadData[7] == 1) {
            return_data = 1;
          } else {
            return_data = -2;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("打开或关闭左侧电磁锁的指令接口异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static byte SumByte(byte[] data, int offset, int length)
  {
    int Result = 0;
    for (int i = offset; i <= offset + length; i++) {
      Result += data[i];
    }
    return (byte)Result;
  }
}
